<template>
  <div>
    <headerNav />
    <div class="mincard">
      <div class="mindWidth fontSize">
        <span @click="$router.push('/')">首页</span> >
        <span @click="toRoute">政策通知</span> >
        <span>详情</span>
      </div>
    </div>
    <div class="informList">
      <div class="mindWidth navOBJ">
        <div class="navContainer navlist">
          <div class="conetent_inform">
            <div class="title">
              <h4>{{ state.DetailsList.title }}</h4>
            </div>
            <div class="from_content">
              <div class="from_t flex space-between items-middle">
                <div>
                  <span>日期: {{ state.DetailsList.releaseTime }}</span>
                  <span>
                    来源:
                    <em
                      @click="departmentXq(state.DetailsList.departmentId)"
                    >{{ state.DetailsList.departmentName }}</em>
                  </span>
                  <button
                    class="button1"
                    :class="state.DetailsList.whetherAttention == 1 ? 'button2' : ''"
                    @click="subscribeEven(state.DetailsList.whetherAttention,state.DetailsList.departmentId)"
                  >{{ state.DetailsList.whetherAttention == 0 ? "订阅" : "已订阅" }}</button>
                  <span class="on-btn" @click="hanldeClick">订阅更多</span>
                </div>
                <div>
                  <el-tooltip
                    v-if="state.DetailsList.whetherCanExport===1"
                    class="item"
                    effect="dark"
                    content="下载"
                    placement="top"
                  >
                    <el-button
                      class="export"
                      @click="exportName"
                    >{{ downName === 1 ? '符合申报条件企业名单' : '符合下一批申报企业名单' }}</el-button>
                  </el-tooltip>
                </div>
              </div>
            </div>
            <div class="conetent" v-html="hcontent"></div>
            <div class="from_b">
              <a v-if="linkSf" :href="state.DetailsList.link" target="_blank">阅读原文</a>
              <span>阅读量</span>
              {{ state.DetailsList.pageViewCount }}
            </div>
          </div>
          <div class="nextinform_content">
            <div class="per_from" v-if="lastData != ''" @click="pieceSwitch(lastData)">
              <span>上一篇</span>
              <p class="singlerows">{{ lastData.title }}</p>
            </div>
            <div class="next_from" v-if="nextData != ''" @click="pieceSwitch(nextData)">
              <span>下一篇</span>
              <p class="singlerows">{{ nextData.title }}</p>
            </div>
          </div>
        </div>
        <div class="observe">
          <div class="img_border">
            <img src="../../assets/Scientific.png" alt />
          </div>
          <div class="reader">
            <div class="title">
              <i></i>大家都在看
            </div>
            <div class="reader_content" v-if="lookingData.length > 0">
              <div
                class="readee_order singlerows"
                v-for="(item, index) in 4"
                @click="lookingEvent(lookingData[index].id, index)"
                :key="index"
              >
                <i></i>
                {{ lookingData[index].title }}
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 订阅已满 -->
    <el-dialog
      :visible.sync="dialogVisible"
      :show-close="false"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="300px"
    >
      <template #footer>
        <div class="contents_qydb">
          <img src="../../static/image/cwhyts2.png" alt class="imgs_qydb" />
          <div class="goVip">
            <div class="top">{{ isDepart === 1 ? '可关注数量已满' : '可下载数量已满' }}</div>
            <div class="bn">成为VIP会员畅享更多权益</div>
          </div>
          <div class="option">
            <span class="spans_qydb" @click="dialogVisible = false">再用看看</span>
            <p class="ps_qydb" @click="toVipPage">升级VIP</p>
          </div>
        </div>
      </template>
    </el-dialog>
    <navBar></navBar>
    <footerNav />
    <!-- 这里是小卡的区域 -->
    <el-dialog v-model="state.isShow" :show-close="false" width="360px">
      <shareCard @clickWechat="nohow" />
    </el-dialog>
    <!-- 登录弹窗 -->
    <login-dialog ref="login" @reload="reload"></login-dialog>
    <!-- 订阅部门选取关键字 -->
    <keyWord ref="keyWord" @onSubmit="attention"></keyWord>
  </div>
</template>

<script>
import navBar from "../../components/navigation.vue";
import shareCard from "../../components/shareCard/index.vue";

import { reactive } from "vue";
import { mapMutations } from "vuex";
import {
  commonRequest,
  postRequest,
  queryPolicyNoticeDetail
} from "../../api/api.js";
import {
  noticeDownloadExcel,
  whetherCanExport,
  whetherDownload,
  cancelAttention,
  departmentAttention
} from "../../api/api.js";
import { sessionTake, decodeUnicode } from "../../util/validate";
import { getCode } from "../../util/address";

import loginDialog from "../../components/login_item/LoginDialog";
import keyWord from "../../components/keyWord.vue"; //部门订阅关键字
export default {
  name: "inform",
  components: {
    shareCard,
    navBar,
    loginDialog,
    keyWord
  },
  head() {
    return {
      title: `${this.state.DetailsList.title}-比目镜`,
      meta: [
        {
          hid: "keywords",
          name: "keywords",
          content: this.state.DetailsList.title
        },
        {
          hid: "description",
          name: "description",
          content: this.state.DetailsList.title
        }
      ]
    };
  },
  data() {
    return {
      dialogVisible: false, // 订阅已满
      state: {
        DetailsList: "",
        isShow: false
      },
      // 上一篇
      lastData: "",
      // 下一篇
      nextData: "",
      lookingData: [],
      linkSf: false,
      hcontent: "",
      policyId: this.$route.query.id,
      isDepart: 1,
      downName: 1 // 下载名单 2公示：下一批符合条件可申报企业名单 1通知：符合申报条件企业名单
    };
  },
  watch: {
    $route(to, from) {
      if (to.name == "inform") {
        this.$router.go(0);
      }
    }
  },
  async asyncData({ app, route, err }) {
    let id = route.params.id;

    let res = await queryPolicyNoticeDetail({ policyId: id });
    let linkSf = false;
    let downName = 1;
    if (res.result.link.indexOf("http") != -1) {
      linkSf = true;
    }
    if (res.result.title.includes("公示")) {
      downName = 2;
    } else {
      downName = 1;
    }
    return {
      policyId: id,
      hcontent: decodeUnicode(res.result.content),
      linkSf: linkSf,
      state: {
        DetailsList: res.result
      },
      downName: downName
    };
  },
  mounted() {
    this.getLooking();
  },
  methods: {
    // 登录刷新当前页
    reload() {
      location.reload();
    },
    toVipPage() {
      window.open("/pageVIP", "_blank");
    },
    ...mapMutations(["setIsShow"]),
    // 关闭微信
    nohow(val) {
      this.state.isShow = val;
    },
    // 点击阅读更多
    hanldeClick() {
      window.open("/subscriptionpage", "_blank");
    },
    // 部门内页
    departmentXq(id) {
      window.open("/departmentInside?id=" + id);
    },
    // 大家都在看
    lookingEvent(id, index) {
      window.open(`/inform/${id}`, "_blank");
    },
    // 上一篇、下一篇切换1上一篇2下一篇
    pieceSwitch(data) {
      window.open(`/inform/${data.id}`);
    },
    handleClose() {
      this.setIsShow(false);
    },
    // 大家都在看
    getLooking() {
      let params = {
        city: sessionTake("cityName"),
        number: 4
      };
      commonRequest(
        "/api/pms/pmsPolicyNoticeInfo/getEveryoneAllWatching",
        params
      ).then(res => {
        this.lookingData = res;
      });
    },
    // 点击 订阅按钮
    subscribeEven(val, id) {
      let token = localStorage.getItem("saasToken");
      if (!token) {
        this.$refs.login.open();
        return;
      }
      if (val == 0) {
        this.$refs.keyWord.open(id);
      } else if (val == 1) {
        // 取消关注
        cancelAttention(id).then(res => {
          if (res.code === 200) {
            this.state.DetailsList.whetherAttention = 0;
            this.$message.success({
              message: "取消订阅成功",
              type: "success"
            });
          } else {
            this.$message.error(res.message);
          }
        });
      }
    },
    // 订阅
    attention(id, keyWord) {
      let params = {
        departmentId: id,
        keywords: keyWord
      };
      departmentAttention(params).then(res => {
        if (res.code === 10001) {
          this.$refs.login.open();
        } else if (res.code === 10002) {
          this.isDepart = 1;
          this.dialogVisible = true;
        } else if (res.code == 200) {
          this.state.DetailsList.whetherAttention = 1;
          this.$message({
            type: "success",
            message: "订阅成功"
          });
        } else {
          this.$message({
            type: "warning",
            message: res.message
          });
        }
      });
    },
    toVip() {
      window.open("/pageVIP", "_balnk");
    },
    decodeUnicode(str) {
      var result = "";
      var code = str.match(/&#(\d+);/g);
      if (code == null) {
        return;
      }
      for (var i = 0; i < code.length; i++) {
        result = result + String.fromCharCode(code[i].replace(/[&#;]/g, ""));
      }
      return result;
    },
    // 获取上一篇、下一篇
    pieceObtain: function() {
      let pathParams = this.$route.query;
      let areaCode = "";
      let province = undefined;
      let city = undefined;
      let area = undefined;
      let departmentNameValue = "";
      let value = "";
      if (pathParams.value) {
        value = pathParams.value;
      }
      if (pathParams.departmentNameValue) {
        departmentNameValue = pathParams.departmentNameValue;
      }
      if (pathParams.provinceValue) {
        province = pathParams.provinceValue;
      } else {
        if (sessionTake("provinceName")) {
          province = sessionTake("provinceName");
        }
      }
      if (pathParams.cityValue) {
        city = pathParams.cityValue;
      } else {
        if (sessionTake("cityName")) {
          city = sessionTake("cityName");
        }
      }
      if (pathParams.areaValue) {
        area = pathParams.areaValue;
        if (province !== "" && city !== "") {
          areaCode = getCode(province, city, area);
        }
      } else {
        if (province !== "" && city !== "") {
          areaCode = getCode(province, city);
        }
      }
      let pageIndex = pathParams.Pagevalue - 1;
      let obj = {
        beginDate: "",
        dq: areaCode,
        endDate: "",
        pageIndex,
        pageSize: 10,
        policyId: "",
        source: departmentNameValue,
        title: value,
        userID: ""
      };
      let index = parseInt(pathParams.index);
      postRequest("/api/es/Policy/queryPolicyList.json", obj).then(res => {
        if (res.retCode == 0) {
          let data = res.data.content;
          if (index > 0) {
            let lastIndex = index - 1;
            this.lastData = {
              id: data[lastIndex].policyId,
              title: data[lastIndex].title,
              index: lastIndex,
              value: value,
              page: pathParams.Pagevalue,
              provinceValue: province,
              cityValue: city,
              areaValue: area | "",
              departmentNameValue: departmentNameValue
            };
          }

          if (index <= 8) {
            let nextIndex = index + 1;
            this.nextData = {
              id: data[nextIndex].policyId,
              title: data[nextIndex].title,
              index: nextIndex,
              value: value,
              page: pathParams.Pagevalue,
              provinceValue: province,
              cityValue: city,
              areaValue: area,
              departmentNameValue: departmentNameValue
            };
          } else {
            let params = {
              beginDate: "",
              dq: areaCode,
              endDate: "",
              pageIndex: pageIndex + 1,
              pageSize: 10,
              policyId: "",
              source: departmentNameValue,
              title: value,
              userID: ""
            };
            postRequest("/api/es/Policy/queryPolicyList.json", params).then(
              res => {
                if (res.retCode == 0) {
                  let nextdata = res.data.content;
                  this.nextData = {
                    id: nextdata[0].policyId,
                    title: nextdata[0].title,
                    index: 0,
                    value: value,
                    page: pathParams.Pagevalue,
                    provinceValue: province,
                    cityValue: city,
                    areaValue: area,
                    departmentNameValue: departmentNameValue
                  };
                }
              }
            );
          }
        }
      });
    },
    canExport() {
      whetherCanExport().then(res => {
        // console.log(res);
        if (res.code == 10001) {
          this.$refs.login.open();
        } else if (res.code == 10006) {
          this.isDepart = 0;
          this.dialogVisible = true;
        } else if (res.code === 200) {
          whetherDownload({ source: "0", policyId: this.policyId });
          noticeDownloadExcel({ policyId: this.policyId }).then(res => {
            let blob = new Blob([res.data]);
            let link = document.createElement("a");
            let fileName = "可申报企业名单.xls";
            link.href = window.URL.createObjectURL(blob);
            link.download = decodeURIComponent(fileName);
            link.click();
          });
        }
      });
    },
    // 下载名单
    exportName() {
      this.canExport();
    },
    toRoute() {
      let params = { province: "", city: "" };
      let provinceName = sessionTake("provinceName");
      let cityName = sessionTake("cityName");
      if (provinceName && cityName) {
        params.province = provinceName;
        params.city = cityName;
        this.$router.push({
          name: "policyNotice",
          params: params
        });
      } else {
        this.$router.push({
          name: "policyNotice"
        });
      }
    }
  }
};
</script>
<style>
.informList a {
  color: blue;
}
</style>
<style lang="less" scoped>
/deep/ .el-button {
  border: none;
}

/* 弹框的开始 */
:deep(.el-dialog__footer) {
  height: 310px;
}

.contents_qydb {
  width: 200px;
  margin-left: 22px;
  margin-top: 20px;

  .goVip {
    display: flex;
    flex-direction: column;
    position: absolute;
    bottom: 55px;
    align-items: center;
    justify-content: center;
    width: 100%;
    left: 0;

    .top {
      font-size: 14px;
      color: #000;
    }

    .bn {
      font-size: 14px;
      color: #1d78de;
    }
  }
}

.el-dialog__footer {
  height: 340px;
  position: relative;
}

.option {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: space-around;
  height: 40px;
  bottom: 10px;
  width: 100%;
  font-size: 14px;
  left: 0;

  .spans_qydb {
    background-color: #bfbfbf;
    padding: 4px 30px;
    color: #fff;
    cursor: pointer;
    border-radius: 30px;
  }

  .ps_qydb {
    border-radius: 30px;

    background-color: #1d78de;
    padding: 4px 30px;
    color: #fff;
    cursor: pointer;
    // bottom: 15px;
    // right: 26px;
    // width: 159px;
    // height: 44px;
    // cursor: pointer;
  }
}

.el-dialog {
  border-radius: 10px;
}

.imgs_qydb {
  width: 100%;
  border-radius: 10px;
}

.navlist {
  margin: 10px 0;
}

.fontSize {
  span {
    font-size: 14px;
    margin: auto 2px;
    cursor: pointer;
  }
}

.navOBJ {
  display: flex;
  justify-content: space-around;
}

.informList {
  background-color: #fff;
}

.navContainer {
  width: 840px;
}

.from_b {
  display: flex;
  align-items: center;
  font-size: 14px;
  margin-bottom: 30px;

  a {
    color: rgb(68, 162, 255);
    margin-right: 20px;
    font-size: 14px;
  }

  .share {
    display: flex;
    align-items: center;
    font-size: 14px;
    cursor: pointer;
  }

  img {
    width: 18px;
    height: 15px;
    margin: 0 10px;
  }

  span {
    font-size: 14px;
    margin-right: 10px;
  }
}

.conetent_inform {
  width: 100%;
  border-bottom: 1px solid #ccc;

  .title {
    h4 {
      font-size: 28px;
    }
  }

  .from_content {
    margin: 30px 0 40px 0;
    color: #999;

    .from_t {
      height: 30px;
      line-height: 30px;

      span {
        font-size: 14px;
        margin-right: 10px;

        em {
          font-style: normal;
          cursor: pointer;
          transition: all 0.2s;

          &:hover {
            color: rgb(68, 162, 255);
          }
        }
      }

      .button1 {
        color: #44a2ff;
        background-color: #fff;
        border-radius: 2px;
        border: 1px solid #44a2ff;
        cursor: pointer;
      }

      .on-btn {
        margin-left: 10px;
        color: rgb(68, 162, 255);
        cursor: pointer;
      }

      .export {
        font-weight: 600;
        font-size: 18px;
        color: red;
        cursor: pointer;
      }

      .button2 {
        background-color: rgb(203, 220, 232);
        border-color: rgb(203, 220, 232);
        color: #fff;
      }
    }
  }

  .conetent {
    margin-bottom: 40px;
    font-size: 16px;
  }
}

.nextinform_content {
  width: 840px;
  overflow: hidden;
  padding: 10px 0;

  .per_from,
  .next_from {
    width: 410px;
    float: left;
    margin-right: 10px;

    span {
      color: #44a2ff;
      font-size: 14px;
    }

    p {
      font-size: 14px;
      cursor: pointer;

      &:hover {
        color: #44a2ff;
      }
    }
  }
}

.observe {
  margin-left: 60px;
  width: 327px;

  .img_border {
    width: 100%;
    margin: 20px auto;

    img {
      width: 300px;
      height: 250px;
    }
  }

  .reader {
    .title {
      font-size: 20px;
    }

    .reader_content {
      margin-top: 15px;
      width: 327px;
      height: 267px;
      border: 1px #000 transparent;
      padding: 20px 5px;

      .readee_order {
        border-bottom: 1px rgba(243, 243, 243, 0.5) solid;
        height: 50px;
        line-height: 50px;
        font-size: 14px;
        cursor: pointer;

        &:hover {
          color: #44a2ff;
        }

        i {
          display: inline-block;
          border: 2px solid #000;
          width: 8px;
          height: 8px;
          margin: 0 10px;
        }
      }
    }
  }
}
</style>
